package com.LostAndFound.service;

import java.sql.SQLException;
import java.util.List;

import com.LostAndFound.dao.RootListDao;
import com.LostAndFound.domain.Root;
import com.LostAndFound.utils.DataSourceUtils;

public class RootListService {
	RootListDao dao = new RootListDao();
	

	public boolean checkAddUsername(String addUsername) {
		int row = 0;
		try {
			row = dao.checkAddUsername(addUsername);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return row>0?true:false;
	}

	public int getTotalRootForEasyUI() {
		int total = 0;
		try {
			total = dao.getTotalRootForEasyUI();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return total;
	}

	public List<Root> getRootListForEasyUI(int pageNumber, int pageSize) {
		List<Root> rootList = null;
		int startIndex = (pageNumber-1)*pageSize;
		try {
			rootList = dao.getRootListForEasyUI(startIndex,pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rootList;
	}
	// 开启事务，把此管理员发布的系统信息一并删除掉
	public boolean handleDeleteRootForEasyUI(String rid) {
		boolean success = false;
		try {
			DataSourceUtils.startTransaction();
			dao.handleDeleteRootForEasyUI(rid);
			dao.deleteSystemInfoByRid(rid);
			success = true;
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return success;
	}
	
	public boolean handleAddMangerEasyUI(Root root) {
		boolean success = false;
		try {
			dao.handleAddMangerEasyUI(root);
			success = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return success;
	}

	public boolean handleEditManagerEasyUI(Root root) {
		boolean success = false;
		try {
			DataSourceUtils.startTransaction();
			dao.handleEditManagerEasyUI(root);
			success = true;
		} catch (SQLException e) {
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return success;
	}

}
